package com.cy.pj.modules.controller;

import com.cy.pj.module.pojo.ResponseResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
public class JsonControoler {
    /**
     * @return
     *
     */
    @RequestMapping("/doConverMapJson")
    @ResponseBody
    public Map<String,Object> doConverMapJson(){
        Map<String,Object> map=new HashMap<>();
        map.put("state", 1);
        map.put("message", "ok");
        return map;
    }
    @RequestMapping("/doPrintJsonToClient")
    @ResponseBody
    public String doPrintJsonToClient() throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("state",1);
        map.put("message", "ok");
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr= objectMapper.writeValueAsString(map);
        System.out.println("jsonStr="+jsonStr);
        return jsonStr;

    }
    @RequestMapping("/doPrintJsonToClient01")
    public void doPrintJsonToClient01(HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("state", 1);
        map.put("message", "红");
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(map);
        System.out.println("jsonStr=" + jsonStr);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        PrintWriter out= response.getWriter();
        out.println(jsonStr);
        out.flush();
    }
    @RequestMapping("/doResponseResult")
    @ResponseBody
    public ResponseResult doResponseResult(){
        ResponseResult result = new ResponseResult();
        result.setCode(257);
        result.setMessage("红");
        return result;
    }

}

